package com.sj.controller;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.map.MapUtil;
import com.sj.common.lang.ApiResultMsg;
import com.sj.common.lang.Const;
import com.sj.utils.ValidateCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoqi
 * Date: 2021-08-02
 * Time: 上午 10:53
 * Description:
 *
 * @author xiaoqi
 */
@RestController
@RequestMapping("/api")
@Api(tags = "认证模块")
public class AuthController extends BaseController {


    @Autowired
    private ValidateCodeUtil validateCodeUtil;

    @ApiOperation(value = "验证码")
    @GetMapping(value = "/captcha")
    public ApiResultMsg kaptcha() throws IOException {
        String key = UUID.randomUUID().toString();
        Map<Object, Object> map = validateCodeUtil.getRandomCodeBase64();
        String base64Image = String.valueOf(map.get("image"));
        String code = String.valueOf(map.get("code"));
        String pre = "data:image/png;base64,";
        base64Image = pre + base64Image;
        redisUtil.hset(Const.CAPTCHA_KEY, key, code, 120);
        return ApiResultMsg.success(
                MapUtil.builder()
                        .put("key", key)
                        .put("base64Image", base64Image)
                        .put("code", code)
                        .build()
        );
    }

}